import axios from "axios";

// 创建axios实例
const service = axios.create({
    timeout: 30 * 1000, // 请求超时时间
});

// request拦截器
service.interceptors.request.use(
    config => {
		// 验证权限
        config.headers["Authorization"] = 'Bearer ';
        return config;
    },
    error => {
        // Do something with request error
        Promise.reject(error);
    },
);

// respone拦截器
service.interceptors.response.use(
    response => {
        if (response.status !== 200) {
            return Promise.reject(response);
        } else {
            const res = response.data;
            if (res.code !== 200) {
                return Promise.reject(response);
            }
            return response.data;
        }
    },
    error => {
        const { response } = error;
        const code = response.data.code;
        return Promise.reject(response.data);
    },
);

export default service;